/**
 *	@file	telseq_support.c
 *	@brief	サポート　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 *
 *	tel_p_hibiki.gmmとtel_p_kotone.gmmは同じID構成になっている前提でプログラムを組む
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "fielddata/script/saveflag.h"
#include "fielddata/maptable/zone_id.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_hibiki.h"
#include "msgdata/msg_tel_p_kotone.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

#define HINT_ZONE_MAX	(73)

static const u16 DATA_HintZoneTbl[HINT_ZONE_MAX] = {
 ZONE_ID_R01, ZONE_ID_R02, ZONE_ID_R03, ZONE_ID_R04,
 ZONE_ID_R05, ZONE_ID_R06, ZONE_ID_R07, ZONE_ID_R08,
 ZONE_ID_R09, ZONE_ID_R10, ZONE_ID_R11, ZONE_ID_R12,
 ZONE_ID_R13, ZONE_ID_R14, ZONE_ID_R15, ZONE_ID_R16,
 ZONE_ID_R17, ZONE_ID_R18, ZONE_ID_W19, ZONE_ID_W20,
 ZONE_ID_W21, ZONE_ID_R22, ZONE_ID_R24, ZONE_ID_R25,
 ZONE_ID_R26, ZONE_ID_R27, ZONE_ID_R28, ZONE_ID_R29,
 ZONE_ID_R30, ZONE_ID_R31, ZONE_ID_R32, ZONE_ID_R33,
 ZONE_ID_R34, ZONE_ID_R35, ZONE_ID_R36, ZONE_ID_R37,
 ZONE_ID_R38, ZONE_ID_R39, ZONE_ID_W40, ZONE_ID_W41,
 ZONE_ID_R42, ZONE_ID_R43, ZONE_ID_R44, ZONE_ID_R45,
 ZONE_ID_R46, ZONE_ID_R47, ZONE_ID_R48, ZONE_ID_T01,
 ZONE_ID_T02, ZONE_ID_T03, ZONE_ID_T04, ZONE_ID_T05,
 ZONE_ID_T06, ZONE_ID_T07, ZONE_ID_T08, ZONE_ID_T09,
 ZONE_ID_T10, ZONE_ID_T11, ZONE_ID_T20, ZONE_ID_T21,
 ZONE_ID_T22, ZONE_ID_T23, ZONE_ID_T24, ZONE_ID_T25,
 ZONE_ID_T26, ZONE_ID_T27, ZONE_ID_T28, ZONE_ID_T29,
 ZONE_ID_T30, ZONE_ID_T31, ZONE_ID_D22R0101, ZONE_ID_D24R0101,
 ZONE_ID_D47,
};

typedef enum{
 FLAG_JOHTO,
 FLAG_SAFARI,
 FLAG_SEKIEI,
 FLAG_KANTO,
 FLAG_MAX,
};

static const u8 DATA_HintRecvFlagTbl[HINT_ZONE_MAX] = {
 FLAG_KANTO,	//	m_supportw_h2_r01
 FLAG_KANTO,	//	m_supportw_h2_r02
 FLAG_KANTO,	//	m_supportw_h2_r03
 FLAG_KANTO,	//	m_supportw_h2_r04
 FLAG_KANTO,	//	m_supportw_h2_r05
 FLAG_KANTO,	//	m_supportw_h2_r06
 FLAG_KANTO,	//	m_supportw_h2_r07
 FLAG_KANTO,	//	m_supportw_h2_r08
 FLAG_KANTO,	//	m_supportw_h2_r09
 FLAG_KANTO,	//	m_supportw_h2_r10
 FLAG_KANTO,	//	m_supportw_h2_r11
 FLAG_KANTO,	//	m_supportw_h2_r12
 FLAG_KANTO,	//	m_supportw_h2_r13
 FLAG_KANTO,	//	m_supportw_h2_r14
 FLAG_KANTO,	//	m_supportw_h2_r15
 FLAG_KANTO,	//	m_supportw_h2_r16
 FLAG_KANTO,	//	m_supportw_h2_r17
 FLAG_KANTO,	//	m_supportw_h2_r18
 FLAG_KANTO,	//	m_supportw_h2_w19
 FLAG_KANTO,	//	m_supportw_h2_w20
 FLAG_KANTO,	//	m_supportw_h2_w21
 FLAG_KANTO,	//	m_supportw_h2_r22
 FLAG_KANTO,	//	m_supportw_h2_r24
 FLAG_KANTO,	//	m_supportw_h2_r25
 FLAG_SEKIEI,	//	m_supportw_h2_r26
 FLAG_SEKIEI,	//	m_supportw_h2_r27
 FLAG_KANTO,	//	m_supportw_h2_r28
 FLAG_JOHTO,	//	m_supportw_h2_r29
 FLAG_JOHTO,	//	m_supportw_h2_r30
 FLAG_JOHTO,	//	m_supportw_h2_r31
 FLAG_JOHTO,	//	m_supportw_h2_r32
 FLAG_JOHTO,	//	m_supportw_h2_r33
 FLAG_JOHTO,	//	m_supportw_h2_r34
 FLAG_JOHTO,	//	m_supportw_h2_r35
 FLAG_JOHTO,	//	m_supportw_h2_r36
 FLAG_JOHTO,	//	m_supportw_h2_r37
 FLAG_JOHTO,	//	m_supportw_h2_r38
 FLAG_JOHTO,	//	m_supportw_h2_r39
 FLAG_JOHTO,	//	m_supportw_h2_w40
 FLAG_JOHTO,	//	m_supportw_h2_w41
 FLAG_JOHTO,	//	m_supportw_h2_r42
 FLAG_JOHTO,	//	m_supportw_h2_r43
 FLAG_JOHTO,	//	m_supportw_h2_r44
 FLAG_JOHTO,	//	m_supportw_h2_r45
 FLAG_JOHTO,	//	m_supportw_h2_r46
 FLAG_SAFARI,	//	m_supportw_h2_r47
 FLAG_SAFARI,	//	m_supportw_h2_r48
 FLAG_KANTO,	//	m_supportw_h2_t01
 FLAG_KANTO,	//	m_supportw_h2_t02
 FLAG_KANTO,	//	m_supportw_h2_t03
 FLAG_KANTO,	//	m_supportw_h2_t04
 FLAG_KANTO,	//	m_supportw_h2_t05
 FLAG_KANTO,	//	m_supportw_h2_t06
 FLAG_KANTO,	//	m_supportw_h2_t07
 FLAG_KANTO,	//	m_supportw_h2_t08
 FLAG_KANTO,	//	m_supportw_h2_t09
 FLAG_SEKIEI,	//	m_supportw_h2_t10
 FLAG_KANTO,	//	m_supportw_h2_t11
 FLAG_JOHTO,	//	m_supportw_h2_t20
 FLAG_JOHTO,	//	m_supportw_h2_t21
 FLAG_JOHTO,	//	m_supportw_h2_t22
 FLAG_JOHTO,	//	m_supportw_h2_t23
 FLAG_JOHTO,	//	m_supportw_h2_t24
 FLAG_JOHTO,	//	m_supportw_h2_t25
 FLAG_JOHTO,	//	m_supportw_h2_t26
 FLAG_JOHTO,	//	m_supportw_h2_t27
 FLAG_JOHTO,	//	m_supportw_h2_t28
 FLAG_JOHTO,	//	m_supportw_h2_t29
 FLAG_JOHTO,	//	m_supportw_h2_t30
 FLAG_JOHTO,	//	m_supportw_h2_t31
 FLAG_JOHTO,	//	m_supportw_h2_d22
 FLAG_JOHTO,	//	m_supportw_h2_d24
 FLAG_SAFARI,	//	m_supportw_h2_d47
};


static u16 sub_CallMsgSelect(u16 zone);

/**
 *	@brief	電話　サポートの会話抽選
 */
u16 PMTelPlayL_Support(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	static const u16 ne_talk[] = {TEL_TK_KOTONE_NE,TEL_TK_HIBIKI_NE};

	if(wk->is_receive){
		wk->seq_code = TEL_SEQ_SUPPORT_RECEIVE;
		return 0;
	}

	//同一マップにいるか？
	if(wk->pbk->map_id == man->zone_id)	{
		wk->seq_code = TEL_SEQ_SIMPLE;
		return ne_talk[man->my_sex];
	}
	//ノーマル発信
	wk->seq_code = TEL_SEQ_SUPPORT_CALL;
	return 0;
}

/**
 *	@brief	サポートの送信会話シーケンス
 */
int PMTelSeq_TypeSupportCall(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);
		break;
	case 1:
		//挨拶出力
		PMTel_MsgPrintGmm(man,man->m_person,m_hibiki_01_1+wk->time_zone);
		break;
	case 2:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		PMTel_MsgPrintGmm(man,man->m_person,sub_CallMsgSelect(man->field_zone));
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}

static u8 sub_RecvMsgSelect(PMTEL_DATA_MAN* man)
{
	int i;
	u8 flag[FLAG_MAX],msg,num = 0,map_open;
	u8* tbl;

	map_open = PGearSv_GetMapOpenLevel(SaveData_GetPGearSave(man->save));

	flag[FLAG_JOHTO] = TRUE;	//最初からオープン

	if(SysFlag_ArriveGet(man->ev_sv,FLAG_ARRIVE_R47)){
		flag[FLAG_SAFARI] = TRUE;	//サファリマップがオープンされたら
	}
	if(map_open > 0){
		flag[FLAG_SEKIEI] = TRUE;	//セキエイマップがオープンされたら
	}
	if(map_open > 1){
		flag[FLAG_KANTO] = TRUE;	//カントーマップがオープンされたら
	}

	tbl = sys_AllocMemoryLo(man->heapID,sizeof(u8)*HINT_ZONE_MAX);
	MI_CpuClear8(tbl,sizeof(u8)*HINT_ZONE_MAX);

	for(i = 0;i < HINT_ZONE_MAX;i++){
		if(flag[DATA_HintRecvFlagTbl[i]]){
			tbl[num++] = i;
		}
	}
	msg = tbl[gf_rand()%num];
	sys_FreeMemoryEz(tbl);
	return msg + m_hibiki_h2_r01;
}

/**
 *	@brief	サポートの着信会話シーケンス
 */
int PMTelSeq_TypeSupportReceive(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);
		break;
	case 1:
		//挨拶出力
		PMTel_MsgPrintGmm(man,man->m_person,m_hibiki_10_1+wk->time_zone);
		break;
	case 2:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		if((gf_rand()%1000) < 500){
			PMTel_MsgPrintGmm(man,man->m_person,m_hibiki_02_1+((gf_rand()%900)/300));
		}else{
			PMTel_MsgPrintGmm(man,man->m_person,sub_RecvMsgSelect(man));
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}


/**
 *	@brief	送信時会話抽選
 */
static u16 sub_CallMsgSelect(u16 zone)
{
	int i;

	for(i = 0;i < HINT_ZONE_MAX;i++){
		if(DATA_HintZoneTbl[i] == zone){
			return m_hibiki_h_r01+i;
		}
	}
	return m_hibiki_02_1+gf_rand()%3;
}
